<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" highlight-current-row @row-click="handleRowClick" @row-contextmenu="rightClick" :height="autoHeight">
      <el-table-column prop="callCmd" label="应用指令" align="center">
        <template slot="header" slot-scope="scope">
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)" style="width: calc(100% - 100px);padding-right: 8px"></el-input>
          <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
          <el-button type="warning" @click="importVisible = true" icon="el-icon-upload2"></el-button>
        </template>
        <template slot-scope="scope">
          <span>{{ scope.row[scope.column.property] }} </span>
          <i v-if="scope.row.appIcon" :class="scope.row.appIcon" style="padding-left: 8px;color: #32a252;"></i>
        </template>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, prev, next"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
    <el-dialog :visible.sync="visible" title="应用信息" width="600px">
      <el-form v-model="entityInfo" label-width="80px" label-position="right">
        <el-form-item label="应用名称" required>
          <el-input v-model="entityInfo.appName"></el-input>
        </el-form-item>
        <el-form-item label="调用指令" required>
          <el-input v-model="entityInfo.callCmd"></el-input>
        </el-form-item>
        <el-form-item label="应用图标" required>
          <idea-icon v-model="entityInfo.appIcon" placeholder="应用图标" style="color: #32a252;"></idea-icon>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(entityInfo)">{{ entityInfo.appId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="importVisible" width="450px" style="text-align: center">
      <el-form v-model="entityInfo" label-width="80px" label-position="right" v-loading="uploadIng">
        <el-form-item label="应用名称">
          <el-input v-model="entityInfo.appName"></el-input>
        </el-form-item>
        <el-form-item label="调用指令">
          <el-input v-model="entityInfo.callCmd"></el-input>
        </el-form-item>
        <el-form-item label="应用图标">
          <idea-icon v-model="entityInfo.appIcon" placeholder="应用图标" style="color: #32a252;"></idea-icon>
        </el-form-item>
        <el-form-item label-width="0">
          <el-upload
              :action="uploadUrl"
              :headers="headers"
              :data="entityInfo"
              :on-success="uploadSuccess"
              :show-file-list="false"
              :before-upload="() => { uploadIng = true;return true}"
              drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import IdeaIcon from "@/components/idea-icon/IdeaIcon";

export default {
  name: "AppTable",
  components: {IdeaIcon },
  mixins: [IdeaTable],
  created() {
    this.queryUrl = '/appInfo/queryPage'
    this.saveUrl = '/appInfo/save'
    this.removeUrl = '/appInfo/delete/'
    this.rowKey = 'appId'
    this.pageInfo.pageSize = 20
    this.rightButtons.splice(0, this.rightButtons.length)
    this.appendRightBtn({ label: '删除', icon: 'el-icon-delete', click: this.removeData })
    this.appendRightBtn({ label: '导出', icon: 'el-icon-download', click: this.exportApp })
    this.loadSource()
  },
  computed: {
    autoHeight() {
      return `calc(100vh - 130px)`
    },
    uploadUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/appInfo/import')
    },
    headers () {
      return { Authorization: 'Bearer ' + this.$AuthStorage.getAccessToken() }
    }
  },
  data () {
    return {
      importVisible: false,
      uploadIng: false
    }
  },
  methods: {
    exportApp(row) {
      const url = this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/appInfo/export/' + row.appId + '?access_token=' + this.$AuthStorage.getAccessToken())
      window.open(url)
    },
    uploadSuccess (response) {
      this.$resultMsg(this, response)
      if (response.code === 200) {
        this.loadSource()
        this.importVisible = false
        this.initEntity()
      }
      this.uploadIng = false
    }
  }
}
</script>

<style scoped>

</style>
